package com.luzhikun.ajax.servlet;

import com.luzhikun.ajax.service.IBmiService;
import com.luzhikun.ajax.service.IBmiServiceImp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "BmiServlet", value = "/bmiServlet")
public class BmiServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取客户端的数据
        String name = request.getParameter("name");
        String weight = request.getParameter("weight");
        String height = request.getParameter("height");
        // 处理数据
        double userWeight=Double.parseDouble(weight);
        double userHeight = Double.parseDouble(height);

        // 调用service层对象处理数据
        IBmiService is=new IBmiServiceImp();
        double bmiCount = is.getBmiCount(userWeight, userHeight);
        String msg="";
        if (bmiCount<=18.5){
            msg="您比较瘦";
        }else if (bmiCount>=18.5 && bmiCount<=23.9){
            msg="您的身体正常";
        }else if (bmiCount>24 && bmiCount<=27 ){
            msg="您的身体比较胖";
        }else {
            msg="您的身体肥胖";
        }
        PrintWriter writer = response.getWriter();

        writer.println("<h3 style='size=40px;color=black'> 尊敬的  "+name+"先生/女士 您的Bmi指数为:  "+bmiCount+"  所以： "+msg+"</h3>");
        // 刷新缓存
        writer.flush();
        // 关闭
        writer.close();

    }

}
